﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

public partial class Account_Transfer : System.Web.UI.Page
{
    public string username { get; set; }
    List<Player> players, lineup;
    User user;
    GameweekTeam currentTeam;
    JsonParser jsonParser;

    protected void Page_Load(object sender, EventArgs e)
    {
        user = (User)Session["UserSession"];
        lineup = new List<Player>();
        jsonParser = new JsonParser();
        getLineup();
        try
        {
            if (user != null)
            {
                players = jsonParser.getAvailablePlayers(user.username);                
                listPlayers();
            }
        }
        catch (NullReferenceException ex)
        {
            Response.Redirect("~/Account/Profile.aspx");
        }
        catch (WebException ex)
        {
            Response.Redirect("~/Account/Profile.aspx");
        }
    }

    private void getLineup()
    {
        currentTeam = jsonParser.getTeam("1", User.Identity.Name);
        lineup.Clear();

        foreach (Player player in currentTeam.keepers)
        {
            lineup.Add(player);
        }
        foreach (Player player in currentTeam.defenders)
        {
            player.position = Position.Defender;
            lineup.Add(player);
        }
        foreach (Player player in currentTeam.midfielders)
        {
            player.position = Position.Midfielder;
            lineup.Add(player);
        }
        foreach (Player player in currentTeam.forwards)
        {
            player.position = Position.Forward;
            lineup.Add(player);
        }
    }

    private void listPlayers()
    {
        int keepers = 0;
        int defenders = 0;
        int midfielders = 0;
        int forwards = 0;

        for (int i = 0; i < lineup.Count; i++)
        {
            Player player = lineup.ElementAt(i);
            String lastName = formatString(player);

            switch (player.position)
            {
                case Position.Keeper:
                    if (keepers == 0)
                    {
                        updatePlayerInfo(pos0name, lastName, "keeper");
                    }
                    else if (keepers == 1)
                    {
                        updatePlayerInfo(pos1name, lastName, "keeper");
                    }
                    keepers++;
                    break;
                case Position.Defender:
                    if (defenders == 0)
                    {
                        updatePlayerInfo(pos2name, lastName, "defender");
                    }
                    else if (defenders == 1)
                    {
                        updatePlayerInfo(pos3name, lastName, "defender");
                    }
                    else if (defenders == 2)
                    {
                        updatePlayerInfo(pos4name, lastName, "defender");
                    }
                    else if (defenders == 3)
                    {
                        updatePlayerInfo(pos5name, lastName, "defender");
                    }
                    else if (defenders == 4)
                    {
                        updatePlayerInfo(pos6name, lastName, "defender");
                    }
                    else
                    {
                        updatePlayerInfo(pos7name, lastName, "defender");
                    }
                    defenders++;
                    break;
                case Position.Midfielder:
                    if (midfielders == 0)
                    {
                        updatePlayerInfo(pos8name, lastName, "midfielder");
                    }
                    else if (midfielders == 1)
                    {
                        updatePlayerInfo(pos9name, lastName, "midfielder");
                    }
                    else if (midfielders == 2)
                    {
                        updatePlayerInfo(pos10name, lastName, "midfielder");
                    }
                    else if (midfielders == 3)
                    {
                        updatePlayerInfo(pos11name, lastName, "midfielder");
                    }
                    else
                    {
                        updatePlayerInfo(pos12name, lastName, "midfielder");
                    }
                    midfielders++;
                    break;
                case Position.Forward:
                    if (forwards == 0)
                    {
                        updatePlayerInfo(pos13name, lastName, "forward");
                    }
                    else if (forwards == 1)
                    {
                        updatePlayerInfo(pos14name, lastName, "forward");
                    }
                    else
                    {
                        updatePlayerInfo(pos15name, lastName, "forward");
                    }
                    forwards++;
                    break;
                default:
                    break;
            }
            //addToLineup(player);
        }
    }
    private void updatePlayerInfo(HtmlGenericControl divName, String lastName, String position)
    {
        divName.InnerHtml = lastName;
        divName.Attributes.Add("class", position);
    }

    private static String formatString(Player player)
    {
        String[] splitName = player.playerName.Split(null);
        String lastName = player.playerName;
        if (splitName.Length > 1)
        {
            lastName = lastName.Substring(0, 1) + "." + splitName[1];
        }
        return lastName;
    }

    void updateDraftDisplay()
    {       
        listPlayers();

        UpdatePanel3.UpdateMode = UpdatePanelUpdateMode.Conditional;
        UpdatePanel3.Update();
    }
}